package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.TreeSet;

@WebServlet( "/stu/deleteStudentServlet")
public class DeleteStudentServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建treeset集合
        TreeSet<String>   ts=new TreeSet<String>();

        String username = req.getParameter("username");
        BufferedReader br=new BufferedReader(new FileReader("D:\\heimastudy\\AdvanceStudent\\WebDemo05\\src\\info.txt"));
        String  len;
        while ((len=br.readLine())!=null){
           ts.add(len);
        }
        //创建输出流
        BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\heimastudy\\AdvanceStudent\\WebDemo05\\src\\info.txt"));
        boolean flag=false;
        for (String t : ts) {
            String[] split = t.split(";");
            System.out.println(split[0]);
            if (username!=null&&username.equals(split[0])){
                flag=true;
            }else {
                bw.write(t);
                bw.newLine();
                bw.flush();
            }
        }
        br.close();
        bw.close();
        if (flag){
            resp.getWriter().write("用户删除成功");
        }else {
            resp.getWriter().write("用户不存在");
        }


    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
